from src.core.db.base import DataBase

"""
lol数据库类
user表
|id|用户ID|
|username|用户名|
|uuid|用户UUID|
|sommerid|用户赛季ID|

games表
|id|游戏ID|
|username|用户名|
|uuid|用户UUID|
|sommerid|用户赛季ID|
|kill|击杀数|
|death|死亡数|
|assist|助攻数|
|time|游戏时长|
"""
class LolDatabase(DataBase):
    def __init__(self, db_url='lol.db'):
        super().__init__(db_url)
        self.connect()

    def init_db(self):
        self.execute_query('''CREATE TABLE IF NOT EXISTS users (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            username TEXT NOT NULL,
            uuid TEXT NOT NULL,
            sommerid TEXT NOT NULL
        )''')

        self.execute_query('''CREATE TABLE IF NOT EXISTS games (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            gameid TEXT NOT NULL,
            username TEXT NOT NULL,
            uuid TEXT NOT NULL,
            sommerid TEXT NOT NULL,
            kill INTEGER NOT NULL,
            death INTEGER NOT NULL,
            assist INTEGER NOT NULL,
            time TEXT NOT NULL
        )''')

        self.execute_query('''CREATE TABLE IF NOT EXISTS loser(
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            gameid TEXT NOT NULL,
            username TEXT NOT NULL,
            uuid TEXT NOT NULL,
            sommerid TEXT NOT NULL,
            kill INTEGER NOT NULL,
            death INTEGER NOT NULL,
            assist INTEGER NOT NULL,
            time TEXT NOT NULL
        )''')

    def add_user(self, username, uuid, sommerid):
        self.execute_query('''INSERT INTO users (username, uuid, sommerid) VALUES (?, ?, ?)''', (username, uuid, sommerid))
        
    def add_game(self, gameid, username, uuid, sommerid, kill, death, assist, time):
        self.execute_query('''INSERT INTO games (gameid, username, uuid, sommerid, kill, death, assist, time) VALUES (?, ?, ?, ?, ?, ?, ?, ?)''', 
                          (gameid, username, uuid, sommerid, kill, death, assist, time))
    def get_user_by_username(self, username):
        cursor = self.execute_query('''SELECT * FROM users WHERE username = ?''', (username,))
        return cursor.fetchall()

    def get_games_by_id(self, gameid):
        cursor = self.execute_query('''SELECT * FROM games WHERE gameid = ?''', (gameid,))
        return cursor.fetchall()